#   D I S T C H E C K _ I N _ S R C _ D I R . C M A K E . I N
# BRL-CAD
#
# Copyright (c) 2012-2025 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###
set(
  dir_initial_status_script
  "file(GLOB_RECURSE SOURCE_ARCHIVE_CONTENTS RELATIVE \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}/*\")
file(WRITE \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/initial_src_contents.txt\" \"\${SOURCE_ARCHIVE_CONTENTS}\")"
)

file(WRITE "${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-initstat.cmake" "${dir_initial_status_script}")

set(
  final_status_script
  "file(GLOB_RECURSE SOURCE_ARCHIVE_CONTENTS RELATIVE \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}\" \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}/*\")
list(REMOVE_ITEM SOURCE_ARCHIVE_CONTENTS .ninja_log)
file(WRITE \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/final_src_contents.txt\" \"\${SOURCE_ARCHIVE_CONTENTS}\")"
)

file(WRITE "${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-finalstat.cmake" "${final_status_script}")

set(
  compare_status
  "file(READ \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/initial_src_contents.txt\" INITIAL_SOURCE_LIST )
file(READ \"${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/final_src_contents.txt\" FINAL_SOURCE_LIST)
string(STRIP \"\${INITIAL_SOURCE_LIST}\" INITIAL_SOURCE_LIST)
string(STRIP \"\${FINAL_SOURCE_LIST}\" FINAL_SOURCE_LIST)
# Ninja keeps a running log in .ninja_log - if we're using ninja itself to run the
# tests, we can't clear this file. Ignore it, as it's not a sign of a problem with
# the clean logic.
list(REMOVE_ITEM FINAL_SOURCE_LIST .ninja_log)
if(NOT \"\${INITIAL_SOURCE_LIST}\" STREQUAL \"\${FINAL_SOURCE_LIST}\")
  set(FINAL_FILES_NOT_IN_INITIAL \${FINAL_SOURCE_LIST})
  set(INITIAL_FILES_NOT_IN_FINAL \${INITIAL_SOURCE_LIST})
  list(REMOVE_ITEM FINAL_FILES_NOT_IN_INITIAL \${INITIAL_SOURCE_LIST})
  list(REMOVE_ITEM INITIAL_FILES_NOT_IN_FINAL \${FINAL_SOURCE_LIST})
  if(INITIAL_FILES_NOT_IN_FINAL)
    message(\"Files present before the build that are no longer present:\")
    foreach(filename \${INITIAL_FILES_NOT_IN_FINAL})
      message(\"\${filename}\")
    endforeach(filename \${INITIAL_FILES_NOT_IN_FINAL})
    message(\"\")
  endif(INITIAL_FILES_NOT_IN_FINAL)
  if(FINAL_FILES_NOT_IN_INITIAL)
    message(\"Files present after the build that were not present initially:\")
    foreach(filename \${FINAL_FILES_NOT_IN_INITIAL})
      message(\"\${filename}\")
    endforeach(filename \${FINAL_FILES_NOT_IN_INITIAL})
    message(\"\")
  endif(FINAL_FILES_NOT_IN_INITIAL)
  message(FATAL_ERROR \"distclean failed in ${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}\")
endif(NOT \"\${INITIAL_SOURCE_LIST}\" STREQUAL \"\${FINAL_SOURCE_LIST}\")
"
)

file(WRITE "${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-eval_status.cmake" "${compare_status}")

add_custom_target(
  distcheck-${TARGET_SUFFIX}
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Extracting TGZ archive..."
  COMMAND ${CMAKE_COMMAND} -E remove distcheck_${TARGET_SUFFIX}.log
  COMMAND ${CMAKE_COMMAND} -E remove_directory distcheck-${TARGET_SUFFIX}
  COMMAND ${CMAKE_COMMAND} -E make_directory distcheck-${TARGET_SUFFIX}
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX} ${CMAKE_COMMAND} -E tar xzf
    ../${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz
  COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-initstat.cmake
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Extracting TGZ archive... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Configuring... "
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME} ${CMAKE_COMMAND} .
    @CMAKE_OPTS@ -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/distcheck-${TARGET_SUFFIX}/${install_dir}
    @TARGET_REDIRECT@
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Configuring... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Compiling using source from TGZ archive..."
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME} @DISTCHECK_BUILD_CMD@
    @TARGET_REDIRECT@
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Compiling using source from TGZ archive... Done."
  COMMAND
    ${CMAKE_COMMAND} -E echo
    "-- distcheck-${TARGET_SUFFIX} - Run build-directory unit, benchmark and regression testing..."
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME} ${CMAKE_COMMAND} --build .
    --target check @TARGET_REDIRECT@
  COMMAND
    ${CMAKE_COMMAND} -E echo
    "-- distcheck-${TARGET_SUFFIX} - Run build-directory unit, benchmark and regression testing... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Install from tgz build directory..."
  COMMAND ${CMAKE_COMMAND} -E make_directory distcheck-${TARGET_SUFFIX}/${install_dir}
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME} @DISTCHECK_INSTALL_CMD@
    @TARGET_REDIRECT@
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Install from tgz build directory... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Test distclean rule..."
  COMMAND ${CMAKE_COMMAND} -E remove -f distcheck-${TARGET_SUFFIX}/${build_dir}/bench/summary
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME} ${CMAKE_COMMAND} --build .
    --target distclean @TARGET_REDIRECT@
  COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-finalstat.cmake
  COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CMakeTmp/distcheck-${TARGET_SUFFIX}-eval_status.cmake
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Test distclean rule... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Clear tgz archive sources and build directory..."
  COMMAND ${CMAKE_COMMAND} -E remove_directory distcheck-${TARGET_SUFFIX}/${CPACK_SOURCE_PACKAGE_FILE_NAME}
  COMMAND
    ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Clear tgz archive sources and build directory... Done."
  COMMAND
    ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Run benchmark testing from tgz archive installed build..."
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX} ${install_dir}/${BIN_DIR}/benchmark run TIMEFRAME=1
    @TARGET_REDIRECT@
  COMMAND
    ${CMAKE_COMMAND} -E chdir distcheck-${TARGET_SUFFIX} ${install_dir}/${BIN_DIR}/benchmark clean @TARGET_REDIRECT@
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Run benchmark testing from installed build... Done."
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Cleanup..."
  COMMAND ${CMAKE_COMMAND} -E remove_directory distcheck-${TARGET_SUFFIX}
  COMMAND ${CMAKE_COMMAND} -E echo "-- distcheck-${TARGET_SUFFIX} - Cleanup... Done."
  COMMENT "[distcheck-${TARGET_SUFFIX}] Performing distcheck - ${TARGET_SUFFIX} configuration..."
  DEPENDS distcheck-source_archives
)
set_target_properties(distcheck-${TARGET_SUFFIX} PROPERTIES FOLDER "BRL-CAD Distribution Checking")
set_target_properties(distcheck-${TARGET_SUFFIX} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
